﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.SystemUI;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;

using ESRI.ArcGIS.Controls;
using System.Runtime.InteropServices;

namespace PlanningGIS.Tools
{
    /// <summary>
    /// 比例尺下拉框
    /// </summary>
    public partial class DropDownMapScale : UserControl, ICommand
    {
        /// <summary>
        /// 
        /// </summary>
        IApplication m_Application = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        public DropDownMapScale()
        {
            InitializeComponent();
            comboBoxEx1.Items.Add("1:500");
            comboBoxEx1.Items.Add("1:2000");
            comboBoxEx1.Items.Add("1:10000");
            comboBoxEx1.Items.Add("1:50000");
            comboBoxEx1.Items.Add("1:250000");
            comboBoxEx1.Items.Add("1:1000000");
        }

        #region ICommand Members        
        /// <summary>
        /// 
        /// </summary>
        int ICommand.Bitmap
        {
            get { return 0; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.Caption
        {
            get { return "比例尺下拉框"; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.Category
        {
            get { return "比例尺"; }
        }
        /// <summary>
        /// 
        /// </summary>
        bool ICommand.Checked
        {
            get { return false; }
        }
        /// <summary>
        /// 
        /// </summary>
        bool ICommand.Enabled
        {
            get
            {
                if (m_Application == null) return false;
                if ((m_Application as IFramework).IsSceneCtlActive) return false;
                if ((m_Application as IFramework).IsMapCtrlactive)
                {
                    if ((m_Application as IFramework).MapControl.SpatialReference == null||  (m_Application as IFramework).MapControl.SpatialReference is ESRI.ArcGIS.Geometry.IUnknownCoordinateSystem)
                    {
                        comboBoxEx1.Text = "";
                        return false ;
                    }
                    else return true ;                  
                }
                else
                {
                    if ((m_Application as IFramework).PageLayoutControl.ActiveView.FocusMap.SpatialReference == null || (m_Application as IFramework).PageLayoutControl.ActiveView.FocusMap.SpatialReference is ESRI.ArcGIS.Geometry.IUnknownCoordinateSystem)
                    {
                        comboBoxEx1.Text = "";
                        return false;
                    }
                    else return true;
                }
               
            }
        }
        /// <summary>
        /// 
        /// </summary>
        int ICommand.HelpContextID
        {
            get { return 0; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.HelpFile
        {
            get { return string.Empty; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.Message
        {
            get { return "比例尺下拉框"; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.Name
        {
            get { return "DropDownMapScale"; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.Tooltip
        {
            get { return "比例尺设置"; }
        }
        /// <summary>
        /// 
        /// </summary>
        void ICommand.OnClick()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hook"></param>
        void ICommand.OnCreate(object hook)
        {
          
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
                IMap pMap;
                if ((m_Application as IFramework).IsMapCtrlactive)
                {
                    IMapControlEvents2_Event mapEvent = (m_Application as IFramework).MapControl as IMapControlEvents2_Event;
                    mapEvent.OnExtentUpdated -= new IMapControlEvents2_OnExtentUpdatedEventHandler(mapEvent_OnExtentUpdated);
                    mapEvent.OnExtentUpdated += new IMapControlEvents2_OnExtentUpdatedEventHandler(mapEvent_OnExtentUpdated);
                    pMap = (m_Application as IFramework).MapControl.Map;
                }
                else
                {
                    IPageLayoutControlEvents_Event pageEvent = (m_Application as IFramework).PageLayoutControl as IPageLayoutControlEvents_Event;
                    pageEvent.OnExtentUpdated -= new IPageLayoutControlEvents_OnExtentUpdatedEventHandler(pageEvent_OnExtentUpdated);
                    pageEvent.OnExtentUpdated += new IPageLayoutControlEvents_OnExtentUpdatedEventHandler(pageEvent_OnExtentUpdated);

                    pMap = (m_Application as IFramework).PageLayoutControl.ActiveView.FocusMap;
                }
                //comboBoxEx1.Text = "1:" + (int)pMap.MapScale;

                (m_Application as IFramework).OnMapPageSwitchEvent -= new OnMapPageSwitchEventHandler(DropDownMapScale_OnMapPageSwitchEvent);
                (m_Application as IFramework).OnMapPageSwitchEvent += new OnMapPageSwitchEventHandler(DropDownMapScale_OnMapPageSwitchEvent);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        void DropDownMapScale_OnMapPageSwitchEvent()
        {
            if (this.Enabled == false) return;
            if ((m_Application as IFramework).IsMapCtrlactive)
            {
                //IMapControlEvents2_Event mapEvent = (m_Application as IFramework).MapControl as IMapControlEvents2_Event;
                //mapEvent.OnExtentUpdated -= new IMapControlEvents2_OnExtentUpdatedEventHandler(mapEvent_OnExtentUpdated);
                //mapEvent.OnExtentUpdated += new IMapControlEvents2_OnExtentUpdatedEventHandler(mapEvent_OnExtentUpdated);
                try
                {
                    comboBoxEx1.Text = "1:" + (int)(m_Application as IFramework).MapControl.ActiveView.FocusMap.MapScale;
                }
                catch (System.Exception)
                {
                }
            }
            else
            {
                //IPageLayoutControlEvents_Event pageEvent = (m_Application as IFramework).PageLayoutControl as IPageLayoutControlEvents_Event;
                //pageEvent.OnExtentUpdated -= new IPageLayoutControlEvents_OnExtentUpdatedEventHandler(pageEvent_OnExtentUpdated);
                //pageEvent.OnExtentUpdated += new IPageLayoutControlEvents_OnExtentUpdatedEventHandler(pageEvent_OnExtentUpdated);
                try
                {
                    comboBoxEx1.Text = "1:" + (int)(m_Application as IFramework).PageLayoutControl.ActiveView.FocusMap.MapScale;
                }
                catch (System.Exception) { }
               
            }
        }
        
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="displayTransformation"></param>
        /// <param name="sizeChanged"></param>
        /// <param name="newEnvelope"></param>
        void pageEvent_OnExtentUpdated(object displayTransformation, bool sizeChanged, object newEnvelope)
        {
            if ((m_Application as IFramework).IsMapCtrlactive)
            {
                if ((m_Application as IFramework).MapControl.MapScale > 10000000)
                {
                    (m_Application as IFramework).MapControl.MapScale = 10000000;
                }
                if ((m_Application as IFramework).MapControl.MapScale < 0.001)
                {
                    (m_Application as IFramework).MapControl.MapScale = 0.001;
                }
                double dblScale = (m_Application as IFramework).MapControl.MapScale;
                long scale = dblScale >= long.MaxValue ? long.MaxValue : (long)dblScale;

                comboBoxEx1.Text = "1:" + scale;
            }
            else
            {
                double dblScale = (m_Application as IFramework).PageLayoutControl.ActiveView.FocusMap.MapScale;
                long scale = dblScale >= long.MaxValue ? long.MaxValue : (long)dblScale;

                comboBoxEx1.Text = "1:" + scale;

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="displayTransformation"></param>
        /// <param name="sizeChanged"></param>
        /// <param name="newEnvelope"></param>
        void mapEvent_OnExtentUpdated(object displayTransformation, bool sizeChanged, object newEnvelope)
        {
            if ((m_Application as IFramework).IsMapCtrlactive)
            {
                if ((m_Application as IFramework).MapControl.MapScale > 10000000)
                {
                    (m_Application as IFramework).MapControl.MapScale = 10000000;
                }
                if ((m_Application as IFramework).MapControl.MapScale < 0.001)
                {
                    (m_Application as IFramework).MapControl.MapScale = 0.001;
                }

                double dblScale = (m_Application as IFramework).MapControl.MapScale;
                long scale = dblScale >= long.MaxValue ? long.MaxValue : (long)dblScale;
               
                comboBoxEx1.Text = "1:" + scale;
            }
            else
            {
                double dblScale = (m_Application as IFramework).PageLayoutControl.ActiveView.FocusMap.MapScale;
                long scale = dblScale >= long.MaxValue ? long.MaxValue : (long)dblScale;

                comboBoxEx1.Text = "1:" + scale;
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxEx1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData != Keys.Enter) return;
            if (m_Application == null) return;
            IActiveView  pActiveView;
            if ((m_Application as IFramework).IsMapCtrlactive)
            {
                pActiveView = (m_Application as IFramework).MapControl.ActiveView;
            }
            else
            {
                pActiveView = (m_Application as IFramework).PageLayoutControl.ActiveView;
            }
            if (comboBoxEx1.Text.Trim() == "")
            {
                comboBoxEx1.Text = "1:" + (int)pActiveView.FocusMap.MapScale;
                return;
            }
            if (comboBoxEx1.Text.Contains(":"))
            {
                string[] strs = comboBoxEx1.Text.Split(":".ToCharArray());
                if (strs.Length != 2)
                {
                    comboBoxEx1.Text = "1:" + (int)pActiveView.FocusMap.MapScale;
                    return;
                }
                if (strs[0].Trim() != "1")
                {
                    comboBoxEx1.Text = "1:" + (int)pActiveView.FocusMap.MapScale;
                    return;
                }
                double dbl;
                if (double.TryParse(strs[1], out dbl))
                {
                    pActiveView.FocusMap.MapScale = dbl;
                    pActiveView.Refresh();
                   
                    
                }
                else
                {
                    comboBoxEx1.Text = "1:" + (int)pActiveView.FocusMap.MapScale;
                }
            }
            else
            {
                double dbl;
                if (double.TryParse(comboBoxEx1.Text.Trim(), out dbl))
                {
                     pActiveView.FocusMap.MapScale = dbl;
                    pActiveView.Refresh();
                }
                else
                {
                    comboBoxEx1.Text = "1:" + (int)pActiveView.FocusMap.MapScale;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
        {
            KeyEventArgs ee=new KeyEventArgs(Keys.Enter);
            comboBoxEx1_KeyDown(null, ee);
        }
    }
}
